﻿using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using Back_end.Models;
using System.Web.Http.Cors;

namespace Back_end.APIControllers
{
    [EnableCors("*", "*", "GET")]
	[RoutePrefix("api/Community")]
    public class CommunitiesController : ApiController
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: api/Communities
		[Route("GetAll")]
        public IQueryable<Community> GetCommunities()
        {
            return db.Communities.OrderBy(c => c.PublishedTime);
        }

        // GET: api/Communities/5
        [ResponseType(typeof(Community))]
		[Route("GetById")]
        public async Task<IHttpActionResult> GetCommunity(string id)
        {
            Community community = await db.Communities.FindAsync(id);
            if (community == null)
            {
                return NotFound();
            }

            return Ok(community);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool CommunityExists(string id)
        {
            return db.Communities.Count(e => e.Id == id) > 0;
        }
    }
}